package com.ctshk.rpc.custom.mapper;

import com.ctshk.rpc.custom.base.CustomBaseMapper;
import com.ctshk.rpc.custom.entity.CustomizedQuotationTouristType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ctshk.rpc.custom.req.QuotationTouristTypeReq;
import com.ctshk.rpc.custom.req.quotation.TouristTypeDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 旅客类型 Mapper 接口
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-13
 */
public interface CustomizedQuotationTouristTypeMapper extends CustomBaseMapper<CustomizedQuotationTouristType> {

    /**
     * 根据类型和主数据id获取旅客信息
     * @param id
     * @param type
     * @return
     */
    @Select("<script>"+
            "SELECT `name`,age_begin,age_end \n" +
            "FROM t_customized_quotation_tourist_type\n" +
            "WHERE is_deleted = 0 AND main_data_id = #{main_data_id}"+
            "<if test=\"type == 1\">AND package_id = #{id}</if> \n" +
            "<if test=\"type == 2\">AND schedule_id = #{id}</if> \n" +
            "</script>")
    TouristTypeDTO queryTouristType(@Param("id") Long id,@Param("main_data_id")Long mainDataId, @Param("type") int type);

    /**
     * 清空旧数据
     * @param scheduleId
     * @param userId
     */
    @Update("UPDATE t_customized_quotation_tourist_type\n" +
            "SET is_deleted = 1,modified_id = #{user_id}\n" +
            "WHERE schedule_id = #{schedule_id} AND is_deleted = 0")
    Integer cleanBySchedule(@Param("schedule_id")Long scheduleId,@Param("user_id")Long userId);

    /**
     * 查询团期套餐旅客类型
     * @param scheduleId
     * @param pkgId
     * @param touristTypeId
     * @return
     */
    @Select("SELECT * \n" +
            "FROM t_customized_quotation_tourist_type\n" +
            "WHERE package_id = #{package_id} AND main_data_id = #{main_data_id} AND is_deleted = 0 LIMIT 1")
    CustomizedQuotationTouristType queryByServiceItem(@Param("schedule_id")Long scheduleId,@Param("package_id") Long pkgId,@Param("main_data_id") Long touristTypeId);

    /**
     * 查询套餐旅客类型列表
     * @param scheduleId
     * @param pkgId
     * @return
     */
    @Select("SELECT main_data_id AS value,name AS label,people_num,age_begin,age_end \n" +
            "FROM t_customized_quotation_tourist_type\n" +
            "WHERE package_id = #{package_id} AND is_deleted = 0")
    List<QuotationTouristTypeReq> queryPackageToutistType(@Param("schedule_id")Long scheduleId,@Param("package_id") Long pkgId);

    /**
     * 查询旅客剩余人数
     * @param scheduleId
     * @return
     */
    @Select("SELECT a.main_data_id AS `value`,a.`name` AS label,a.age_begin,a.age_end,(b.total_people - b.order_people) AS peopleNum \n" +
            "FROM t_customized_quotation_tourist_type a \n" +
            "LEFT JOIN t_customized_trip_schedule_tourist_count b ON a.main_data_id = b.tourist_type_id\n" +
            "WHERE a.schedule_id = #{schedule_id} AND a.is_deleted = 0 AND b.schedule_id = #{schedule_id} GROUP BY a.main_data_id\n")
    List<QuotationTouristTypeReq> queryRemainPeople(@Param("schedule_id")Long scheduleId);

    /**
     * 查询旅客剩余人数
     * @param scheduleId
     * @return
     */
    @Select("SELECT a.main_data_id AS `value`,a.`name` AS label,a.age_begin,a.age_end,a.people_num \n" +
            "FROM t_customized_quotation_tourist_type a \n" +
            "WHERE a.schedule_id = #{schedule_id} AND a.is_deleted = 0 GROUP BY a.main_data_id\n")
    List<QuotationTouristTypeReq> queryRemainPeople2(@Param("schedule_id")Long scheduleId);

    /**
     * 询价单旅客数组
     * @param inquiryId
     * @return
     */
    @Select("SELECT a.main_data_id AS `value`,a.`name` AS label,a.age_begin,a.age_end,a.people_num \n" +
            "FROM t_customized_quotation_tourist_type a \n" +
            "WHERE a.inquiry_id = #{inquiry_id} AND a.is_deleted = 0 GROUP BY a.main_data_id\n")
    List<QuotationTouristTypeReq> queryByInquiry(@Param("inquiry_id") Long inquiryId);
}
